<template>
  <div class="app-container">

    <h2 align="center">发布课程</h2>

    <el-steps :active="active" process-status="wait" finish-status="success" align-center style="margin-bottom: 4px">
      <el-step title="填写课程基本信息"></el-step>
      <el-step title="创建课程大纲"></el-step>
      <el-step title="最终发布"></el-step>
    </el-steps>


    <div class="ccInfo">
      <img :src="coursePublish.cover" />
      <div class="main">
        <h2>{{ coursePublish.title }}</h2>
        <p><span>所属分类：{{ coursePublish.oneSubject }} / {{ coursePublish.twoSubject }}</span></p>
        <p><span>课程讲师：{{ coursePublish.teacherName }}</span></p>
        <p class="gray"><span>共 <b>{{ coursePublish.lessonNum }}</b> 课时</span></p>
        <p class="price"><span>￥{{ coursePublish.price }}</span></p>
      </div>
    </div>

    <el-button-group class="stepActions">
      <el-button style="margin-top: 12px;" @click="previous">返回修改</el-button>
      <el-button :disabled="saveBtnDisabled" type="primary" style="margin-top: 12px;" @click="onPublishHandler">发布课程</el-button>
    </el-button-group>

<!--    <el-form ref="form" :model="courseInfo" label-width="120px">
      <el-form-item label="课程标题">
        <el-input v-model="courseInfo.title" placeholder="示例: 机器学习项目课"></el-input>
      </el-form-item>
      <el-form-item label="一级分类"><el-input v-model="courseInfo.oneSubject"></el-input></el-form-item>
      <el-form-item label="二级分类"><el-input v-model="courseInfo.twoSubject"></el-input></el-form-item>
      <el-form-item label="讲师"><el-input v-model="courseInfo.teacherName"></el-input></el-form-item>
      <el-form-item label="课程封面">{{  }}</el-form-item>
      <el-form-item label="课程价格">
        <el-input-number :min="0" v-model="courseInfo.price" controls-position="right"/>
      </el-form-item>

      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" style="margin-top: 12px;" @click="previous">返回修改</el-button>
        <el-button :disabled="saveBtnDisabled" type="success" style="margin-top: 12px;" @click="onPublishHandler">发布课程</el-button>
      </el-form-item>
    </el-form>-->

  </div>
</template>

<script>
import courseService from "@/api/edu/course";
import chapterService from "@/api/edu/chapter";

export default {
  data() {
    return {
      courseId: '',
      active: 3,
      loading: false,
      saveBtnDisabled: false,
      pageTitle: '',

      coursePublish: {
        id: '',
        title: '',
        cover: '',
        price: '',
        lessonNum: 0,
        teacherName: '',
        oneSubject: '',
        twoSubject: '',
        description: '',
      }
    };
  },
  created() {
    this.getCourseId()
    this.fetchData()
  },
  methods: {
    getCourseId() {
      if (this.$route.params && this.$route.params.id) {
        this.courseId = this.$route.params.id
      }
    },
    fetchData() {
      this.loading = true
      // this.fullscreenLoading = true
      if (this.$route.params && this.$route.params.id) {
        const courseId = this.$route.params.id
        courseService.prePublishCourse(courseId).then(res => {
          this.loading = false
          // this.fullscreenLoading = false
          const info = res.data.data
          if (info) {
            this.coursePublish = info
          } else {
            this.$message({
              type: "error",
              message: "未找到课程信息, 请添加课程信息!"
            })
            this.$router.push(`/course/info`)
          }
        })
      }
    },

    previous() {
      // if (this.active++ > 2) this.active = 1;
      this.$router.push(`/course/chapter/${this.courseId}`)
    },
    onPublishHandler() {
      // if (this.active++ > 2) this.active = 1;
      // this.$router.push("/course/info")
      this.publish(this.courseId)
    },
    publish(courseId) {
      courseService.publishCourse(courseId).then(res => {
        this.$message({
          type: "success",
          message: "发布成功"
        })
        this.$router.push("/course/list")
      })
    }
  }
}
</script>

<style scoped>
.ccInfo {
  background: #f5f5f5;
  padding: 20px;
  overflow: hidden;
  border: 1px dashed #ddd;
  margin-bottom: 40px;
  position: relative;
}
.ccInfo img {
  background: #d6d6d6;
  width: 500px;
  height: 240px;
  display: block;
  float: left;
  border: none;
}
.ccInfo .main {
  margin-left: 520px;
}

.ccInfo .main h2 {
  font-size: 28px;
  margin-bottom: 30px;
  line-height: 1;
  font-weight: normal;
}
.ccInfo .main p {
  margin-bottom: 10px;
  word-wrap: break-word;
  line-height: 24px;
  max-height: 48px;
  overflow: hidden;
}

.ccInfo .main p {
  margin-bottom: 10px;
  word-wrap: break-word;
  line-height: 24px;
  max-height: 48px;
  overflow: hidden;
}
.ccInfo .main .price {
  left: 540px;
  bottom: 20px;
  line-height: 1;
  font-size: 28px;
  color: #d32f24;
  font-weight: normal;
  position: absolute;
}

.stepActions {
  float: right;
  position: relative;
  margin-bottom: 30px;
}
</style>
